﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Blog.Core;

namespace Blog.Win
{
    public partial class BloggerForm : Form
    {
        public BloggerForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            try
            {
                _tabControl.SelectedIndex = 0;
                _htmlTabControl.SelectedIndex = 0;

                _themesBindingSource.DataSource = ViewController.GetThemes();
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
                Close();
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            _themesBindingSource.EndEdit();

            base.OnClosing(e);
        }

        private void RefreshMessages()
        {
            try
            {
                Theme theme = _themesBindingSource.Current as Theme;
                MessageCollection collection = theme == null ? null : theme.GetMessages(true);
                
                _treeView.Tag = collection;
                _treeView.Nodes.Clear();
                _messageTextBox.Clear();

                FillTree(collection, null, null);

                _treeView.SelectedNode = null;
                _treeView_AfterSelect(null, null);
                _treeView.ExpandAll();
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

        private void RefreshBrowser()
        {
            Theme theme = _themesBindingSource.Current as Theme;

            if (_webBrowser.Document != null)
            {
                _webBrowser.Document.OpenNew(true);
                _webBrowser.Document.Write(theme == null || string.IsNullOrEmpty(theme.Description) ? "<html><body></body></html>" : theme.Description);
            }
            else
                _webBrowser.DocumentText = theme == null ? string.Empty : theme.Description;
        }

        private void _themesBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            RefreshBrowser();
        }

        private void HtmlTabControlSelectedIndexChanged(object sender, EventArgs e)
        {
            if (_htmlTabControl.SelectedIndex == 0)
                RefreshBrowser();
        }

        private void _tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_tabControl.SelectedIndex == 1)
                RefreshMessages();
        }

        private void FillTree(MessageCollection collection, Core.Message parentMessage, TreeNode parentNode)
        {
            foreach (Core.Message mes in collection.GetLevel(
                parentMessage == null ? 0 : parentMessage.MessageTreeId, 
                parentMessage == null ? 0 : parentMessage.ID))
            {
                TreeNode node = AddNode(parentNode, mes);

                FillTree(collection, mes, node);
            }
        }

        private TreeNode AddNode(TreeNode parentNode, Core.Message mes, bool insert = false)
        {
            TreeNode node = GenerateNode(mes);
            if (parentNode == null)
            {
                if (insert)
                    _treeView.Nodes.Insert(0, node);
                else
                    _treeView.Nodes.Add(node);
            }
            else
            {
                if (insert)
                    parentNode.Nodes.Insert(0, node);
                else
                    parentNode.Nodes.Add(node);
            }

            return node;
        }

        private TreeNode GenerateNode(Core.Message mes)
        {
            TreeNode node = new TreeNode(mes.MessageText);
            node.Tag = mes;
            return node;
        }

        private void _addMessageButton_Click(object sender, EventArgs e)
        {
            MessageCollection collection = _treeView.Tag as MessageCollection;
            if (collection == null || string.IsNullOrEmpty(_messageTextBox.Text))
                return;

            try
            {
                Core.Message newMessage = collection.AddNew() as Core.Message;
                newMessage.MessageText = _messageTextBox.Text;
                newMessage.Save();

                _treeView.SelectedNode = AddNode(null, newMessage, true);
                _messageTextBox.Clear();
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

        private void _deleteButton_Click(object sender, EventArgs e)
        {
            MessageCollection collection = _treeView.Tag as MessageCollection;
            Core.Message selectMessage = _treeView.SelectedNode == null ? null : _treeView.SelectedNode.Tag as Core.Message;

            if (collection == null || selectMessage == null)
                return;

            try
            {
                selectMessage.SetDeleted();
                collection.Remove(selectMessage);

                _treeView.SelectedNode.Remove();
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

        private void _answerButton_Click(object sender, EventArgs e)
        {
            MessageCollection collection = _treeView.Tag as MessageCollection;
            if (collection == null || string.IsNullOrEmpty(_messageTextBox.Text))
                return;

            Core.Message selectMessage = _treeView.SelectedNode == null ? null : _treeView.SelectedNode.Tag as Core.Message;

            try
            {
                Core.Message newMessage = collection.AddNew() as Core.Message;
                newMessage.MessageText = _messageTextBox.Text;
                newMessage.MessageTreeId = selectMessage == null ? 0 : selectMessage.MessageTreeId;
                newMessage.ParentId = selectMessage == null ? 0 : selectMessage.ID;
                newMessage.Save();

                _treeView.SelectedNode = AddNode(_treeView.SelectedNode, newMessage);
                _messageTextBox.Clear();
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

        private void _treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            Core.Message selectMessage = _treeView.SelectedNode == null ? null : _treeView.SelectedNode.Tag as Core.Message;
            _answerButton.Enabled = _deleteButton.Enabled = selectMessage != null;
        }
    
    }
}
